local weiguan = fk.CreateSkill{
  name = "sclan__weiguan",
}

local DIY = require "packages/diy_utility/diy_utility"

local prompts = {}
prompts.obtain = "sclan__weiguan-obtain"

Fk:loadTranslationTable{
  ["sclan__weiguan"] = "威贯",
  [":sclan__weiguan"] = "每名角色回合结束时，若中央区牌数小于你的手牌数，你可以交换两者，视为使用【万箭齐发】。" ..
    "结算后，你获得中央区的X张牌（X为此牌造成伤害次数），若全部获得，本轮此技能失效。",

  [prompts.obtain] = "威贯：请选择中央区的%arg张牌获得",
}

weiguan:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

weiguan:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(weiguan.name) and player:getHandcardNum() > #DIY.getCenterCards(player.room)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = weiguan.name })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:moveCards({
      ids = player:getCardIds("h"),
      from = player,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonExchange,
      skillName = weiguan.name,
      proposer = player,
      moveVisible = true
    }, {
      ids = DIY.getCenterCards(player.room),
      toArea = Card.PlayerHand,
      to = player,
      moveReason = fk.ReasonExchange,
      skillName = weiguan.name,
      proposer = player,
      moveVisible = true
    })
    if player.dead then return end

    local card = Fk:cloneCard("archery_attack")
    card.skillName = weiguan.name
    if table.find(room.alive_players, function (p) return player:canUseTo(card, p) end) then
      room:useCard{
        card = card,
        from = player,
        tos = card:getAvailableTargets(player),
        skillName = weiguan.name,
        extra_data = {
          end_id = room.logic:getCurrentEvent().id
        }
      }
    end
    if player.dead then return end
  end
})

weiguan:addEffect(fk.CardUseFinished, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(weiguan.name) and
      data.card.skillName == weiguan.name
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local invalid
    local num = #room.logic:getActualDamageEvents(999, function (e)
      return e.data.card and e.data.card.skillName and e.data.card.skillName == weiguan.name
    end, Player.HistoryTurn, room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true).id)

    if num >= #DIY.getCenterCards(room) then
      invalid = true
      num = #DIY.getCenterCards(room)
    end

    local cards = room:askToChooseCards(player, {
      min = num,
      max = num,
      flag = { card_data = { {"#CenterArea", DIY.getCenterCards(room)} } },
      target = player,
      skill_name = weiguan.name,
      prompt = prompts.obtain .. ":::" .. num,
    })
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, weiguan.name, nil, true, player)

    if not player.dead and invalid then
      room:invalidateSkill(player, weiguan.name, "-round", weiguan.name)
    end
  end
})

return weiguan